package max.ubitest.pipe.data
{
	import flash.events.Event;
	
	import max.ubitest.pipe.Config;
	
	[Bindable]
	public class ComponentData extends Data
	{
		public var direction:int;
		public var sides:Array;
		public var amount:int;
		public var width:int;
		public var height:int;
		public var cellData:CellData;
		
		private var _type:int;
		private var _path:String;
		
		public function ComponentData(value:Object=null)
		{
			super(value);
		}
		
		[Bindable("typeChange")]
		public function get type():int
		{
			return _type;
		}

		public function set type(value:int):void
		{
			_type = value;
			_path = Config.IMG_BASE + 'component' + type + '.png';
			dispatchEvent(new Event("typeChange"));
		}
		
		[Bindable("typeChange")]
		public function get path():String{
			return _path;
		}
		
		public function rotate():void{
			direction = direction >= Directions.LEFT ? 0:direction + 1;
			var i:int;
			var temp:Array = sides.concat();
			if(type == ComponentTypes.RESERVOIR){
				switch(direction){
					case Directions.UP:
						sides = [0, 1, 0, 0, 0, 0, 0, 0];
						break;
					case Directions.RIGHT:
						sides = [0, 0, 0, 1, 0, 0, 0, 0];
						break;
					case Directions.DOWN:
						sides = [0, 0, 0, 0, 0, 1, 0, 0];
						break;
					case Directions.LEFT:
						sides = [0, 0, 0, 0, 0, 0, 0, 1];
						break;
					default:
						sides = [0, 1, 0, 0, 0, 0, 0, 0];
						break;
				}
			}else{
				for(i = sides.length - 1;i>=0;i--){
					if(i == 0){
						sides[i] = temp[sides.length - 1];
					}else{
						sides[i] = temp[i - 1];
					}
				}
			}
			sides = sides.concat();
		}
		
		override public function clone():Data{
			var data:Data = super.clone();
			(data as ComponentData).direction = Directions.UP;
			switch(type){
				case ComponentTypes.CORNOR:
					(data as ComponentData).sides = [1, 1, 0, 0];
					break;
				case ComponentTypes.ONE_PORT:
				case ComponentTypes.WATER_PUMP:
					(data as ComponentData).sides = [1, 0, 0, 0];
					break;
				case ComponentTypes.STRAIGHT:
					(data as ComponentData).sides = [1, 0, 1, 0];
					break;
				case ComponentTypes.THREE_PORT:
					(data as ComponentData).sides = [1, 1, 0, 1];
					break;
				case ComponentTypes.RESERVOIR:
					(data as ComponentData).sides = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
					break;
			}
			return data;
		}
	}
}